package com.evil.kingdom.task.timer;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.evil.kingdom.pojo.entity.action.Battle;
import com.evil.kingdom.pojo.entity.action.BattlePerson;
import com.evil.kingdom.service.battle.impl.AutoBattleService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Timer;
import java.util.stream.Collectors;

/**
 * 战斗-个人版 定时器
 *
 * @author liyang
 * @date 2024-06-25 11:02
 */
@Slf4j
public class BattleTimerTask extends BaseTimerTask<Battle, BattleTimerTask, BattlePerson<?>> {
    public static final Logger battleLogger = LoggerFactory.getLogger("KINGDOM_BATTLE_LOGGER");

    public BattleTimerTask(Timer timer, Battle battle, StopCallback<BattleTimerTask> stopCallback, RunCallback<Battle, BattleTimerTask, BattlePerson<?>> runCallback) {
        super(timer, battle, stopCallback, runCallback);
    }

    public static List<BattleTimerTask> build(List<Battle> battles, StopCallback<BattleTimerTask> stopCallback, RunCallback<Battle, BattleTimerTask, BattlePerson<?>> runCallback) {
        DateTime dateTime = DateUtil.offsetSecond(new Date(), 20);
        return battles.stream().map(battle -> {
            Timer newTimer = new Timer();
            BattleTimerTask battleTimerTask = new BattleTimerTask(newTimer, battle, stopCallback, runCallback);
            // 定时器延迟0毫秒后开始执行任务，之后每time执行一次
            newTimer.schedule(battleTimerTask, dateTime, calSpeedTime(battle.getPerson().getBase().getSpeed()));
            return battleTimerTask;
        }).collect(Collectors.toList());
    }

    @Override
    public BattlePerson<?> task() {
        Battle battle = this.getT();
        new AutoBattleService().move(battle);

        if (battle.getFlight()) {
            if (null == battle.getAttackTimerTask()) {
                Timer newTimer = new Timer();
                AttackTimerTask attackTimerTask = new AttackTimerTask(newTimer, battle, c -> {
                }, (t, c, r) -> {
                    BattlePerson<?> result = null;
                    switch (r) {
                        case ATTACK_VICTORY -> result = t.getEnemy();
                        case ATTACK_DEFEAT -> result = t.getPerson();
                    }
                    this.getRunCallback().callback(battle, this, result);
                    if (null != result) {
                        battle.setAttackTimerTask(null);
                        battle.setFlight(false);
                    }
                });
                // 定时器延迟0毫秒后开始执行任务，之后每time执行一次
                newTimer.schedule(attackTimerTask, 0, calAttackTime(battle.getPerson().getBase().getAttackSpeed()));

                battle.setAttackTimerTask(attackTimerTask);
            }
        } else {
            if (null != battle.getAttackTimerTask()) {
                battle.setAttackTimerTask(null);
                battle.setFlight(false);
            }
        }
        return null;
    }

    @Override
    public BattleTimerTask get() {
        return this;
    }

    private static long calAttackTime(Double attackSpeed) {
        if (null == attackSpeed) {
            attackSpeed = 1D;
        }
        if (attackSpeed < 0) {
            attackSpeed = 0.1;
        }
        if (attackSpeed > 10) {
            attackSpeed = 10D;
        }
        // attackSpeed > 1 时 公式=1000 + (1 - x) * 10000
        return (long) (1000 + (1 - attackSpeed) * (attackSpeed > 1 ? 100 : 10000));
    }

    private static long calSpeedTime(Double speed) {
        if (null == speed) {
            speed = 1D;
        }
        if (speed < 0) {
            speed = 0.1;
        }
        if (speed > 10) {
            speed = 10D;
        }
        // attackSpeed > 1 时 公式=1000 + (1 - x) * 10000
        return (long) (1000 + (1 - speed) * (speed > 1 ? 100 : 10000));
    }
}
